package com.stone.persistent.recyclerview.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.stone.persistent.recyclerview.R;
import com.stone.persistent.recyclerview.adapter.viewholder.CarouselViewHolder;
import com.stone.persistent.recyclerview.adapter.viewholder.EmptyViewHolder;
import com.stone.persistent.recyclerview.adapter.viewholder.FeedsViewHolder;
import com.stone.persistent.recyclerview.adapter.viewholder.LoadingViewHolder;
import com.stone.persistent.recyclerview.adapter.viewholder.MenuViewHolder;

import org.jetbrains.annotations.NotNull;


public class MainListAdapter extends RecyclerView.Adapter {
    private static final String TAG = "MainListAdapter";
    private AppCompatActivity context;
    private LayoutInflater inflater;
    private boolean tabsLoaded;
    private LoadingTabsListener loadingTabsListener;
    private static int VIEW_TYPE_CAROUSEL = 0;
    private static int VIEW_TYPE_MENU = 1;
    private static int VIEW_TYPE_SEC_KILL_PREFIX = 2;
    private static int VIEW_TYPE_SEC_KILL_CONTENT = 3;
    private static int VIEW_TYPE_TODAY_RECOMMEND = 4;
    private static int VIEW_TYPE_NEW_YEAR_STREET = 5;
    private static int VIEW_TYPE_FEEDS = 6;
    private static int VIEW_TYPE_LOADING_TABS = 7;

    public int getItemViewType(int position) {
        int viewType;
        switch (position) {
            case 0:
                viewType = VIEW_TYPE_CAROUSEL;
                break;
            case 1:
                viewType = VIEW_TYPE_MENU;
                break;
            case 2:
                viewType = VIEW_TYPE_SEC_KILL_PREFIX;
                break;
            case 3:
                viewType = VIEW_TYPE_SEC_KILL_CONTENT;
                break;
            case 4:
                viewType = VIEW_TYPE_TODAY_RECOMMEND;
                break;
            case 5:
                viewType = VIEW_TYPE_NEW_YEAR_STREET;
                break;
            case 6:
                viewType = this.tabsLoaded ? VIEW_TYPE_FEEDS : VIEW_TYPE_LOADING_TABS;
                break;
            default:
                viewType = -1;
        }

        return viewType;
    }

    @NotNull
    public RecyclerView.ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        View itemView;
        Log.e(TAG, "onCreateViewHolder: " + tabsLoaded);
        switch (viewType) {
            case 0:
                itemView = this.inflater.inflate(R.layout.item_main_carousel, parent, false);
                viewHolder = new CarouselViewHolder(itemView, context);
                break;
            case 1:
                itemView = this.inflater.inflate(R.layout.item_main_menu, parent, false);
                viewHolder = new MenuViewHolder(itemView, context);
                break;
            case 2:
                itemView = this.inflater.inflate(R.layout.item_sec_kill_prefix, parent, false);
                viewHolder = new EmptyViewHolder(itemView);
                break;
            case 3:
                itemView = this.inflater.inflate(R.layout.item_sec_kill_content, parent, false);
                viewHolder = new EmptyViewHolder(itemView);
                break;
            case 4:
                itemView = this.inflater.inflate(R.layout.item_today_recommend, parent, false);
                viewHolder = new EmptyViewHolder(itemView);
                break;
            case 5:
                itemView = this.inflater.inflate(R.layout.item_new_year_street, parent, false);
                viewHolder = new EmptyViewHolder(itemView);
                break;
            case 7:
                itemView = this.inflater.inflate(R.layout.item_loading_footer, parent, false);
                viewHolder = new LoadingViewHolder(itemView);
                break;
            default:
                itemView = this.inflater.inflate(R.layout.item_main_feeds, parent, false);
                viewHolder = new FeedsViewHolder(itemView);
                break;
        }
        return viewHolder;
    }

    public int getItemCount() {
        return 7;
    }

    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof LoadingViewHolder) {
            if (loadingTabsListener != null) {
                loadingTabsListener.invoke();
            }
        } else if (holder instanceof CarouselViewHolder) {
            ((CarouselViewHolder) holder).invalidate();
        } else if (holder instanceof FeedsViewHolder) {
            ((FeedsViewHolder) holder).initAdapter(context);
        }
    }

    public void setLoadingTabsListener(LoadingTabsListener listener) {
        this.loadingTabsListener = listener;
    }

    public void onTabsLoaded() {
        tabsLoaded = true;
        notifyItemChanged(7);
    }

    public void setTabsLoaded(boolean tabsLoadeds) {
        tabsLoaded = tabsLoadeds;
    }


    public MainListAdapter(AppCompatActivity context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public interface LoadingTabsListener {
        void invoke();
    }

}
